package com.browan.constellation;

import java.util.ArrayList;
import java.util.List;
import com.browan.constellation.configuration.ResourceManager;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;

public class GridViewConstellation extends BaseActivity
{
	// private Animation[] alpha_animation;
	String TAG = getClass().getSimpleName();
	private GridView gridview;

	private List<String> list = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridview_constellation);
		gridview = (GridView) findViewById(R.id.gridview);

		gridview.setVerticalFadingEdgeEnabled(true);

		gridview.setVerticalScrollBarEnabled(false);

		// alpha_animation = new Animation[12];
		//
		// for (int i = 0; i < 12; i++)
		// {
		// alpha_animation[i] = AnimationUtils.loadAnimation(this,
		// R.anim.shake);
		// }
		gridview.setAdapter(new GridViewAdapter(this));

		list.add("星座传说");
		list.add("今日运势");
		list.add("明日运势");
		list.add("本月运势");

		gridview.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				// view.startAnimation(alpha_animation[position]);
				// gridview.performLongClick();
				final int pos = position;

				Builder childer = new AlertDialog.Builder(GridViewConstellation.this);
				childer.setTitle(ResourceManager.NAMES[position]);

				ArrayAdapter<String> adapter = new ArrayAdapter<String>(GridViewConstellation.this,
						R.layout.simple_list_item_1, R.id.text1, list);

				childer.setAdapter(adapter, new OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog, int which)
					{
						dispather(which, pos);
					}
				});
				childer.create().show();

			}
		});
	}

	public void dispather(int item, int selectedPosition)
	{

		Intent intent = new Intent();
		switch (item)
		{
		case 0:
			intent.setClass(this, ConDetial.class);
			intent.putExtra(Intent.EXTRA_UID, selectedPosition);
			startActivity(intent);
			break;

		case 1:
			intent.setClass(this, ShowLasterNews.class);
			intent.putExtra(ShowLasterNews.DATE_TIME, 0);
			intent.putExtra(ShowLasterNews.SELECT_INDEX, selectedPosition + 1);
			startActivity(intent);
			break;

		case 2:
			intent.setClass(this, ShowLasterNews.class);
			intent.putExtra(ShowLasterNews.DATE_TIME, 2);
			intent.putExtra(ShowLasterNews.SELECT_INDEX, selectedPosition +1);
			startActivity(intent);
			break;
			
		case 3:
			intent.setClass(this, ShowLasterNews.class);
			intent.putExtra(ShowLasterNews.DATE_TIME, 1);
			intent.putExtra(ShowLasterNews.SELECT_INDEX, selectedPosition + 1);
			startActivity(intent);
			break;
		}
	}

	@Override
	public boolean onContextItemSelected(MenuItem item)
	{

		boolean ret = true;

		int selectedPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position;

		Intent intent = new Intent();
		switch (item.getItemId())
		{
		case Menu.FIRST:
			intent.setClass(this, ConDetial.class);
			intent.putExtra(Intent.EXTRA_UID, selectedPosition);
			startActivity(intent);
			break;

		case Menu.FIRST + 1:
			intent.setClass(this, ShowLasterNews.class);
			intent.putExtra(ShowLasterNews.DATE_TIME, 0);
			intent.putExtra(ShowLasterNews.SELECT_INDEX, selectedPosition);
			startActivity(intent);
			break;

		case Menu.FIRST + 2:
			intent.setClass(this, ShowLasterNews.class);
			intent.putExtra(ShowLasterNews.DATE_TIME, 1);
			intent.putExtra(ShowLasterNews.SELECT_INDEX, selectedPosition);
			startActivity(intent);
			break;

		default:
			ret = super.onContextItemSelected(item);
			break;
		}
		return ret;
	}

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
	}

	@Override
	protected void onStop()
	{
		super.onStop();
	}

	private class GridViewAdapter extends BaseAdapter
	{

		public GridViewAdapter(Context c)
		{

			mContext = c;
		}

		private Context mContext;
		private int mThumbIds[] = ResourceManager.IMAGE_ICON_ID;

		@Override
		public int getCount()
		{
			return mThumbIds.length;
		}

		@Override
		public Object getItem(int position)
		{
			return mThumbIds[position];
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{

			if (null == convertView)
			{
				convertView = LayoutInflater.from(mContext).inflate(R.layout.gridview_item, null);
			}

			ImageView imageView = (ImageView) convertView.findViewById(R.id.img);
			TextView tv = (TextView) convertView.findViewById(R.id.name);
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setImageResource(mThumbIds[position]);
			imageView.setAlpha(200);

			tv.setText(ResourceManager.NAMES[position]);
			return convertView;
		}
	}
}